<?php

namespace bc\service;

use bc\service\base\Service;
use bc\model\coupons\CouponsModel;
use bc\model\coupons\CouponsUserModel;
use yii\db\Query;
use yii\base\Object;
use bc\entity\Order;
use bc\entity\Coupon;

class CouponService extends Service
{
	
	const COUPON_TYPE_REDBAG      = 1;//红包
	const COUPON_TYPE_SUBTRACTION = 2;//减
	
	
	const COUPON_RANGE_SINGLE     = 1;//单品
	const COUPON_RANGE_GROUP      = 2;//产品组
	const COUPON_RANGE_FULL       = 3;//全场
	
	/**
	 * 
	 * @var \stdClass $coupon
	 */
	protected $coupon;
	
	/**
	 * 
	 * @var \stdClass $order
	 */
	protected $order;
	
	/**
	 * @return the $coupon
	 */
	public function getCoupon()
	{
		return $this->coupon;
	}

	/**
	 * @return the $order
	 */
	public function getOrder()
	{
		return $this->order;
	}

	/**
	 * @param stdClass $coupon
	 */
	public function setCoupon(Coupon $coupon)
	{
		$this->coupon = $coupon;
	}

	/**
	 * @param stdClass $order
	 */
	public function setOrder( Order $order)
	{
		$this->order = $order;
	}

	public function __construct()
	{
		$this->model = new CouponsModel();
	}
	
	
	
	/**
	 * 获取可用的优惠券
	 * @param array $params
	 */
	public function getAvailableList($params)
	{
		$params = $this->setWhere($params);
		$this->model->columns = $this->columns();
		return $this->model->getFrontendList($params, CouponsUserModel::getTableSchema()->name .'.coupons_id');
	}
	
	/**
	 * 
	 * @param string $where
	 */
	protected function setWhere($where)
	{
		$query = new Query();
		
		$query->where([CouponsUserModel::getTableSchema()->name . '.status' => 0, CouponsUserModel::getTableSchema()->name . '.is_del' => 0]);
		if($where['user_id'])
		{
			$query->andWhere([CouponsUserModel::getTableSchema()->name . '.user_id' => $where['user_id']]);
		}
		
		if($where['product_id'])
		{
			$query->andWhere(['in', CouponsModel::getTableSchema()->name . '.product_id', explode(',', $where['product_id'])]);
		}
		
		if($where['product_group_id'])
		{
			$query->andWhere('in', [CouponsModel::getTableSchema()->name . '.product_group_id', explode(',', $where['product_group_id'])]);
		}
		
		if($where['use_start_time'])
		{
			$query->andWhere(['<=', CouponsModel::getTableSchema()->name . '.use_start_time' ,$where['use_start_time']]);
		}
		
		if($where['use_end_time'])
		{
			$query->andWhere(['>=', CouponsModel::getTableSchema()->name . '.use_end_time' ,$where['use_end_time']]);
		}
		if($where['product_price'])
		{
			$query->andWhere(CouponsModel::getTableSchema()->name . '.coupons_full_money <= ' . intval( $where['product_price']) . ' OR '. CouponsModel::getTableSchema()->name . '.coupons_full_money = 0');
		}
		
		
		return str_replace('SELECT * WHERE', '', $query->createCommand()->getRawSql());
	}
	
	
	/**
	 * 优惠券结算
	 * @param object $coupon
	 * @param object $order
	 * @return float 
	 * 
	 */
	public function checkout()
	{
		$coupon = $this->getCoupon();
		$order = $this->getOrder();
		
		$price = floatval($order->price);
		
		if($coupon instanceof bc\entity\Coupon)
		{
			throw new \Exception('counpon unkow Object');
		}
		
		if($order instanceof bc\entity\Order)
		{
			throw new \Exception('order unkow Object');
		}
		
		if($this->getAvailable())
		{
			if( in_array($this->getCoupon()->coupons_type, [self::COUPON_TYPE_REDBAG, self::COUPON_TYPE_SUBTRACTION]) )
			{
				$price = floatval($order->price - $coupon->coupons_money) ;
			}
		}
		
		if($price < 0)
			$price = 0.00;
		
		return $price;
	}
	
	
	/**
	 * 判断优惠券是否可用
	 * @return boolean
	 */
	protected function getAvailable()
	{
		$coupon = $this->getCoupon();
		$order = $this->getOrder();
		
		$available = false;
		
		if($coupon->coupons_type == self::COUPON_TYPE_REDBAG)
		{
			if($coupon->coupons_use_target == self::COUPON_RANGE_FULL)
			{
				$available = true;
			}
			else if($coupon->coupons_use_target == self::COUPON_RANGE_GROUP)
			{
				if($coupon->product_group_id == $order->group_id)
				{
					$available = true;
				}
			}
			else if($coupon->coupons_use_target == self::COUPON_RANGE_SINGLE)
			{
				if($coupon->product_id == $order->product_id)
				{
					$available = true;
				}
			}
		}
		elseif($coupon->coupons_type == self::COUPON_TYPE_SUBTRACTION)
		{
			if($coupon->coupons_use_target == self::COUPON_RANGE_FULL)
			{
				$available = true;
			}
			else if($coupon->coupons_use_target == self::COUPON_RANGE_GROUP)
			{
				if($coupon->product_group_id == $order->group_id)
				{
					$available = true;
				}
			}
			else if($coupon->coupons_use_target == self::COUPON_RANGE_SINGLE)
			{
				if($coupon->product_id == $order->product_id)
				{
					$available = true;
				}
			}	
		}
		
		return $available;
		
	}
	
	
	
	
	/**
	 * 
	 */
	public function columns()
	{
		$couponsColumns = CouponsModel::getTableSchema()->getColumnNames();
		foreach ($couponsColumns as $key=>$column)
		{
			if($key != 0)
				$sort[] = CouponsModel::getTableSchema()->name . '.' . $column;
		}
		
		
		$sort[] = CouponsUserModel::getTableSchema()->name . '.user_id AS userid';
		$sort[] = CouponsUserModel::getTableSchema()->name . '.coupons_user_id as cuserid';
		
		return $sort;
	}
	
	
}

